import datetime
import sys
from loguru import logger as _logger
from datetime import datetime
from .utils import get_project_root
from .config import EnvMode, config

def define_print_level(print_level = "INFO", logfile_level = "DEBUG", name:str = None):
    root_dir = get_project_root()
    formatted_date = datetime.now().strftime("%Y%m%d%H%M%S")
    log_name = (
        f"{name}_{formatted_date}" if name else f"{formatted_date}"
    )

    _logger.remove()

    _logger.add(
        sys.stderr,
        level = print_level
    )

    _logger.add(
        str(root_dir / "logs" / f"{log_name}.log"),
        level = logfile_level
    )
    
    return _logger

print_level = "INFO"

if config.env.mode == EnvMode.LOCAL:
    print_level = "WARNING"

logger = define_print_level(print_level)

if __name__ == "__main__":
    logger.info("Starting application")
    logger.debug("Debug message")
    logger.warning("Warning message")
    logger.error("Error message")
    logger.critical("Critical message")

    try:
        raise ValueError("Test error")
    except Exception as e:
        logger.exception(f"An error occurred: {e}")
